package com.hs.telepass.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.hs.telepass.model.TelepassProfiles;
import com.hs.telepass.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

public class TelepassProfilesDatabase {
    private Context mContext;

    public static final String TABLE_NAME = "telepassProfiles";
    public static final String COLUMN_KEY_ID = "profileID";
    public static final String COLUMN_HOST = "host";
    public static final String COLUMN_PATH = "path";
    public static final String COLUMN_USERNAME = "username";
    public static final String COLUMN_ENCRYPTED_PASSWORD = "encryptedPassword";

    public TelepassProfilesDatabase(Context mContext) {
        this.mContext = mContext;
    }

    public long addRecord(TelepassProfiles profile) {
        List<TelepassProfiles> recordtList = new ArrayList<TelepassProfiles>();
        recordtList.add(profile);
        return addRecords(recordtList).get(0);
    }

    public List<Long> addRecords(List<TelepassProfiles> recordList) {
        SQLiteDatabase db = DatabaseHelper.getInstance(mContext);
        List<Long> idList = new ArrayList<Long>();
        for (TelepassProfiles profile : recordList) {
            ContentValues cv = new ContentValues();
            cv.put(COLUMN_HOST, profile.getHost());
            cv.put(COLUMN_PATH, profile.getPath());
            cv.put(COLUMN_USERNAME, profile.getUsername());
            cv.put(COLUMN_ENCRYPTED_PASSWORD, profile.getEncryptedPassword());
            long id = db.insert(TABLE_NAME, null, cv);
            idList.add(id);
        }

        return idList;
    }

    public List<TelepassProfiles> getProfiles() {
        SQLiteDatabase db = DatabaseHelper.getInstance(mContext);
        List<TelepassProfiles> list = new ArrayList<TelepassProfiles>();
        String sql = "select * from " + TABLE_NAME;
        Cursor cursor = db.rawQuery(sql, null);
        while(cursor.moveToNext()) {
            TelepassProfiles profile = new TelepassProfiles();
            int profileId = cursor.getInt(cursor.getColumnIndex(COLUMN_KEY_ID));
            String host = cursor.getString(cursor.getColumnIndex(COLUMN_HOST));
            String path = cursor.getString(cursor.getColumnIndex(COLUMN_PATH));
            String username = cursor.getString(cursor.getColumnIndex(COLUMN_USERNAME));
            String encryptedPassword = cursor.getString(cursor.getColumnIndex(COLUMN_ENCRYPTED_PASSWORD));

            profile.setProfileID(profileId);
            profile.setHost(host);
            profile.setPath(path);
            profile.setUsername(username);
            profile.setEncryptedPassword(encryptedPassword);

            list.add(profile);
        }
        cursor.close();
        return list;
    }

    /**
     * 更新数据状态
     * */
    public void updateProfile(int id, String username, String password) {
        SQLiteDatabase db = DatabaseHelper.getInstance(mContext);

        ContentValues cv = new ContentValues();
        if(!StringUtil.isEmpty(username))
            cv.put(COLUMN_USERNAME, username);

        cv.put(COLUMN_ENCRYPTED_PASSWORD, password);

        db.update(TABLE_NAME, cv, COLUMN_KEY_ID + "=?",
                new String[] {String.valueOf(id) });
    }

    public int deleteProfile(int id){
        SQLiteDatabase db = DatabaseHelper.getInstance(mContext);
        String where = COLUMN_KEY_ID + "=?";
        String[] whereArgs = {String.valueOf(id)};
        return db.delete(TABLE_NAME, where, whereArgs);
    }

/*


    public void clearRecords(){
        SQLiteDatabase db = DatabaseHelper.getInstance(mContext);
        DatabaseHelper dh = DatabaseHelper.getHelper(mContext);
        dh.clearLoginTable(db);
    }

    public int deleteRecord(long id){
        SQLiteDatabase db = DatabaseHelper.getInstance(mContext);
        String where = COLUMN_KEY_ID + "=?";
        String[] whereArgs = {String.valueOf(id)};
        return db.delete(TABLE_NAME, where, whereArgs);
    }*/


}
